package com.yu.subject.domain.handler.subject;

import com.yu.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectTypeHandlerFactory
 * @description
 * @date 2024/12/24 22:36
 * @since jdk11
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    @Resource
    // 当spring启动时，所有的实现类都会被注入到这个集合中
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    private Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    public SubjectTypeHandler getHandler(Integer subjectType) {
        return handlerMap.get(SubjectInfoTypeEnum.getByCode(subjectType));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList) {
            handlerMap.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);
        }
    }
}
